package 测试;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class 双排序 {

	public static void main(String[] args) {
		int[] a = { 0, 9, 0, 8, 9, 10 };
		int[] b = { 5, 1, 1, 1, 100, 100 };
		System.out.println(Arrays.toString(a));
		System.out.println(Arrays.toString(b));

		extracted(a, b);

		System.out.println(Arrays.toString(a));
		System.out.println(Arrays.toString(b));

	}

	public static void extracted(int[] a, int[] b) {
		TreeMap<Integer, ArrayList<Integer>> hm = new TreeMap<Integer, ArrayList<Integer>>();
		for (int i = 0; i < b.length; i++) {
			if (hm.containsKey(b[i])) {
				hm.get(b[i]).add(a[i]);
			} else {
				hm.put(b[i], new ArrayList<Integer>());
				hm.get(b[i]).add(a[i]);
			}
		}
		for (Map.Entry x : hm.entrySet()) {
			Integer key = (Integer) x.getKey();
			ArrayList<Integer> value = (ArrayList<Integer>) x.getValue();
			Collections.sort(value, new Comparator<Integer>() {
				@Override
				public int compare(Integer o1, Integer o2) {
					return o1 < o2 ? 1 : -1;
				}
			});
			hm.put(key, value);
		}
		System.out.println(hm.toString());
		int idx = 0;
		for (Map.Entry x : hm.entrySet()) {
			Integer key = (Integer) x.getKey();
			ArrayList<Integer> value = (ArrayList<Integer>) x.getValue();
			for (int i = 0; i < value.size(); i++) {
				a[idx] = value.get(i);
				b[idx++] = key;
			}
		}
	}
}
